 package com.webside.wap.listener;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ServletContextAware;

import com.webside.wap.common.mapper.BdictionaryMapper;
import com.webside.wap.common.mapper.BsensitivewordMapper;
import com.webside.wap.common.model.Bdictionary;
import com.webside.wap.listener.BaseData;
import com.webside.wap.listener.vo.Channel;

public class InitialDataListener implements InitializingBean, ServletContextAware{
	@Autowired
	private BdictionaryMapper bdictionaryMapper;
	@Autowired
	private BsensitivewordMapper bsensitivewordMapper;
	private String imgBasePath;

	@Override
	public void setServletContext(ServletContext servletContext) {
		BaseData.img_base_path=imgBasePath;
		
		BaseData.channels = new ArrayList<Channel>();
		
		Bdictionary dic = new Bdictionary("CHANNEL",null);
		List<Bdictionary> dicChannels = bdictionaryMapper.queryByTypeAndCodeAsc(dic,null);
		Channel channel;
		for (int i = 0; i < dicChannels.size(); i++) {
			channel=new Channel();
			Bdictionary dicChannel=dicChannels.get(i);
			channel.setChannel(dicChannel);
			channel.setName(dicChannel.getName());
			channel.setCode(dicChannel.getCode());
			Bdictionary dic1 = new Bdictionary("DRAMA_TYPE", dicChannel.getCode());
			channel.setDramaTypes(bdictionaryMapper.queryByTypeAndCodeAsc(dic1,null));
			Bdictionary dic2 = new Bdictionary("SERIES_KIND", dicChannel.getCode());
			channel.setSeriesKinds(bdictionaryMapper.queryByTypeAndCodeAsc(dic2,null));
			BaseData.channels.add(channel);
			System.out.println("init data finished!");
		}
		
		//敏感词加载
		BaseData.Sensitivewords=new HashSet<String>();
		//TODO 从数据库查询出所有敏感词，循环遍历到BaseData.Sensitivewords
		List<String> sensitiveword= bsensitivewordMapper.queryAllSensitiveWord();
		BaseData.Sensitivewords.addAll(sensitiveword);
			
		
		
	}

	public void afterPropertiesSet() throws Exception {

	}

	public String getImgBasePath() {
		return imgBasePath;
	}

	public void setImgBasePath(String imgBasePath) {
		this.imgBasePath = imgBasePath;
	}
	
}
